<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use App\Http\Requests\AskUserRequest;

class AskUserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        // echo 22;
        //每页显示
        $num = $request->input('num',8);
        //判断是否有关键词
        if($request->input('keywords')){

            $users = DB::table('askuser')->where('name','like','%'.$request->input('keywords').'%')->orderBy('time','desc')->paginate($num);

        }else{

            //查询数据
            $users = DB::table('askuser')->orderBy('time','desc')->paginate($num);
            
        }

        $data = $request->except('page');
        // dd($data);
        return view('Admin.askuser.index',['users'=>$users,'data'=>$data]);
    }

    //救助信息填写表单
    public function getAdd()
    {
        //查询申请类型
        $cates = DB::table('ask')->get();

        return view('Admin.askuser.add',['cates'=>$cates]);
    }

    //录入申请信息

    public function postInsert(Request $request)

    public function postInsert(AskUserRequest $request)
    {
        // dd($request->all());
        $data = $request->except('_token');
        $time = time();
        $data['time'] = $time;

        //数据库添加
        $res = DB::table('askuser')->insert($data);
        if($res){
            return redirect('/admin/askuser/index')->with('success','添加成功');
        }else{
            return back()->with('error','添加失败');
        }
    }


    //修改
    public function getEdit($id)
    {
        // echo 22;
        //查数据
        $users = DB::table('askuser')->where('id',$id)->first();
        $types = DB::table('ask')->get();

        return view('Admin.askuser.edit',['users'=>$users,'types'=>$types]);
    }

    //修改添加
    public function postUpdate(Request $request)
    {
        // dd($request->all());
        $data = $request->except('_token');

        //修改
        $res = DB::table('askuser')->where('id',$request->id)->update($data);

        if($res){
            return redirect('/admin/askuser/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }
   
   //删除
    public function postDel(Request $request)
    {
        $res = DB::table('askuser')->where('id','=',$request->id)->delete();
         echo $res;
    }

    //审核
    public function getChange($id)
    {
        $data['id'] = $id;
        $data['status']=1;
        $res = DB::table('askuser')->where('id',$data['id'])->update($data);
        if($res){
            return redirect('/admin/askuser/index')->with('success','审核通过');
        }else{

            return back()->with('error','审核未成功');
         //查询当前状态
        $res = DB::table('askuser')->where('id',$id)->first();

       //判断
        if($res->status == 1)
        {
            $data['id'] =$id;
            $data['status'] = 0;

           $num = DB::table('askuser')->where('id',$id)->update($data);

        }else{
            
            $data['id'] =$id;
            $data['status'] = 1;

           $num = DB::table('askuser')->where('id',$id)->update($data);

        }

        if($num){
            return redirect('/admin/askuser/index')->with('success','显示状态修改成功');
        }else{
             return back()->with('error','显示状态修改失败');
        }
    }
}
